package com.edu.scau.test;

import java.io.File;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.edu.scau.dao.OrderFormDao;
import com.edu.scau.dao.implement.OrderFormDaoImp;
import com.edu.scau.dao.implement.ShoppingCartDaoImpl;
import com.edu.scau.dao.implement.UserDaoImpl;
import com.edu.scau.dao.util.SessionFactoryUtil;
import com.edu.scau.entity.Commodity;
import com.edu.scau.entity.OrderForm;
import com.edu.scau.entity.ShoppingCart;
import com.edu.scau.entity.Sold;
import com.edu.scau.entity.User;
import com.edu.scau.service.LoginService;
import com.edu.scau.service.Register;

public class test {
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}


	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	public void run(){
		
		
		
//		ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure(new File("src/hibernate.cfg.xml")).build();
//		Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
//		SchemaExport schemaExport = new SchemaExport();
//		schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
	Session session= sessionFactory.openSession();
	Transaction tx=session.beginTransaction();
	
	Commodity commodity=new Commodity();
	commodity.setAverageNumber(10);
	commodity.setClassification("hjq");
	commodity.setDescription("hjq's");
	commodity.setId(1);
	commodity.setImageUrl("/hjq");
	commodity.setName("hjq");
	commodity.setPrice(2.0);
	commodity.setShoppingCarts(null);
	commodity.setSolds(null);
	
	session.save(commodity);
	
	User user=new User();
	user.setAddress("hjq's home");
	user.setBalance(100.0);
	user.setEmail("hjq's email");
	user.setId(1);
	user.setName("hjq");
	user.setOrderForms(null);
	user.setPassword("hjq's password");
	user.setPhoneNumber("188141");
	user.setSex("woman");
	user.setShoppingCarts(null);

	session.save(user);
	
	OrderForm orderForm = new OrderForm();
	orderForm.setCreateTime("2012-01-10");
	orderForm.setOrderFormId(1);
	orderForm.setSolds(null);
	orderForm.setUser(user);
	session.save(orderForm);
	
	ShoppingCart sc=new ShoppingCart();
	sc.setCommodity(commodity);
	sc.setNumber(10);
	sc.setUser(user);
	
	session.save(sc);
	
	Sold sold=new Sold();
	sold.setCommodity(commodity);
	sold.setNumber(10);
	sold.setOrderForm(orderForm);
	session.save(sold);
	
	tx.commit();
	session.close();
	}
	
	public static void main(String[] args) {
		FileSystemXmlApplicationContext context = 
				new FileSystemXmlApplicationContext("D:\\Program Files\\git import\\ShoppingWebAPP\\src\\applicationContext.xml");

		SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
		//		test test = new test();
//		test.setSessionFactory(sessionFactory);
		//test.run();
		
//		OrderFormDaoImp orderFormDao = new OrderFormDaoImp();
//		orderFormDao.setFactory(sessionFactory);
//		UserDaoImpl Dao = new UserDaoImpl();
//		Dao.setFactory(sessionFactory);
//		
//		System.out.println(orderFormDao.queryByUserId(1));
//		System.out.println(Dao.save(new User()));
//		UserDaoImpl Dao = new UserDaoImpl();
//		Dao.setFactory(sessionFactory);
//		System.out.println(Dao.save(new User()));
//		System.out.println(Dao.get(1).getAddress());
//		User user=new User();
//		user.setId(2);user.setName("mou");
//		Commodity com=new Commodity();
//		com.setId(2);
////		System.out.println(Dao.update(user));'
//		ShoppingCartDaoImpl dao2=new ShoppingCartDaoImpl();
//		dao2.setFactory(sessionFactory);
//		ShoppingCart cart=new ShoppingCart();
//		cart.setCommodity(com);cart.setUser(user);cart.setNumber(5);
//		//ShoppingCart cart2=(ShoppingCart)dao2.queryByUserId(2).get(0);
//		//ShoppingCart cart3=(ShoppingCart)dao2.queryByUserId(2).get(1);
//		List<ShoppingCart> list=dao2.queryByUserId(2);
//		for(ShoppingCart sp:list){
//			System.out.println(sp.getUser().getId());
//		}
//	
//		//System.out.println((ShoppingCart)(dao2.queryByUserId(2).get(0)).getCommodity());
////		
	//OrderFormDaoImp dao=new OrderFormDaoImp();
	//dao.setFactory(sessionFactory);
//		
//		List<User> ls=new ArrayList<User>();
		//dao.queryByUserId(2);
//		if(ls==null)
//			return;
//		for(OrderForm form : ls){
//			System.out.println(form.getUser().getName());
//		}
		//LoginService log=new LoginService();
		//System.out.println(log.LoginCheck(2, "000001"));
		//Register reg=new Register();
		//System.out.println(reg.UserRegister(3, "cxd", "111111", "cxd_home", "10086", "12306", "man"));
		
	}

}
